<template>
	<view>
		<view class="invoiceWrapper">
			<!-- 发票类型 -->
			<view class="invoiceItemWrapper">
				<view class="title">
					选择发票类型
				</view>
				<view class="content">
					<radio-group @change="invoiceTypeRadioChange">
						<label class="radioBox" v-for="(item, index) in invoiceTypes" :key="item.value">
							<radio style="transform:scale(0.6)" :value="item.value" color="#009CF9"
								:checked="item.value === currentInvoiceType" />
							<text style="margin-right: 16upx;">{{item.name}}</text>
						</label>
					</radio-group>
				</view>
			</view>
			<!-- 发票类型 -->
			<view class="invoiceItemWrapper">
				<view class="title">
					发票详情
				</view>
				<view class="content" @click="chooseInvoiceTitle">
					<view class="titleInfo" v-if="currentInvoiceTitleType==0">
						<text class="subtitle">选择抬头</text>
						<u-icon name="arrow-right" size="12"></u-icon>
					</view>
					<view v-else class="titleInfo">
						<view>
							<view class="contentItem">
								<text>抬头类型</text>
								<text>{{currentInvoiceTitleType|invoiceTitleType}}</text>
							</view>
							<view class="contentItem">
								<text class="subtitle">发票抬头</text>
								<text>{{invoiceTitle}}</text>
							</view>
							<view class="contentItem" v-if="currentInvoiceTitleType == 1">
								<text class="subtitle">公司税号</text>
								<text>{{dutyParagraph}}</text>
							</view>
						</view>
						<u-icon name="arrow-right" size="12"></u-icon>
					</view>
				</view>
			</view>
			<!-- 发票金额 -->
			<view class="invoiceItemWrapper">
				<view class="title">
					发票总金额
				</view>
				<view class="content">
					<view class="invoicePrice">
						<text>开票总金额</text>
						<text>¥{{totalPrice}}</text>
					</view>
				</view>
			</view>
			
			<!-- 发票方式 -->
			<view class="invoiceItemWrapper">
				<view class="title">
					开票方式
				</view>
				<view class="content">
					<view>
						<radio-group @change="invoiceWayeRadioChange">
							<label class="radioBox" v-for="(item, index) in invoiceWays" :key="item.value">
								<radio style="transform:scale(0.6)" :value="item.value" color="#009CF9"
									:checked="item.value === currentInvoiceWay" />
								<text style="margin-right: 16upx;">{{item.name}}</text>
							</label>
						</radio-group>
					</view>
			
					<view class="infoItem" v-if="currentInvoiceWay === '1'">
						<text class="subtitle">电子邮箱</text>
						<u--input v-model="email" placeholder="请输入收件人名称" border="none" fontSize="13"></u--input>
					</view>
			
					<view v-if="currentInvoiceWay === '2'">
						<view class="infoItem">
							<text class="subtitle">收件人</text>
							<u--input v-model="consignee" placeholder="请输入收件人名称" border="none" fontSize="13"></u--input>
						</view>
						<view class="infoItem">
							<text class="subtitle">收件地址</text>
							<u--input v-model="consigneeAddress" placeholder="请输入收件地址" border="none" fontSize="13">
							</u--input>
						</view>
						<view class="infoItem">
							<text class="subtitle">联系电话</text>
							<u--input v-model="contactPhone" placeholder="请输入联系电话" border="none" fontSize="13"></u--input>
						</view>
					</view>
			
				</view>
			</view>
		</view>
		
      
         <view class="bottomBtn" @click="next">
         	下一步
         </view>
		 
		 <u-popup :show="showInvoiceAlert" closeable :round="10" @close="showInvoiceAlert=false">
		 	<view class="inviceAlertWrapper">
		 		<view class="contentWrapper">
		 			<view class="title">
		 				{{currentInvoiceWay == '1' ? '开具电子发票' : '开具纸质发票'}}
		 			</view>
		 			<view class="invoiceContent">
		 				<view class="invoiceItem">
		 					<text>发票方式</text>
		 					<text>{{currentInvoiceWay == '1' ? '电子发票' : '纸质发票'}}</text>
		 				</view>
		 				<view class="invoiceItem">
		 					<text>抬头类型</text>
		 					<text>{{currentInvoiceTitleType|invoiceTitleType}}</text>
		 				</view>
		 				<view class="invoiceItem">
		 					<text>发票抬头</text>
		 					<text>{{invoiceTitle}}</text>
		 				</view>
						<!-- 电子发票需显示 -->
		 				<view v-if="currentInvoiceWay == '1'" class="invoiceItem">
		 					<text>电子邮箱</text>
		 					<text>{{email}}</text>
		 				</view>
						<!-- 纸质发票需显示 -->
						<view v-if="currentInvoiceWay == '2'">
							<view class="invoiceItem">
								<text>收件人</text>
								<text>{{consignee}}</text>
							</view>
							<view class="invoiceItem">
								<text>收件地址</text>
								<text>{{consigneeAddress}}</text>
							</view>
							<view class="invoiceItem">
								<text>联系电话</text>
								<text>{{contactPhone}}</text>
							</view>
						</view>
						
						<view style="margin: 30upx; 0">
							<u-line></u-line>
						</view>
		 				<view class="remind">
		 					<text v-if="currentInvoiceWay == '1'">请确认邮箱无误，电子发票将在系统开具1-2个工作日后发送至您的 邮箱，请注意查收；</text>
		 					<text> 提示：过路过桥费、高速费、停车费等，请您向收费站和停车场索取 相关发票。</text>
		 				</view>
		 			</view>
		 		</view>
		 		
		 		<view class="buttonWrapper">
		 			<view class="button" @click="commitInvoice">
		 				确定提交
		 			</view>
		 		</view>
		 	</view>
		 </u-popup>
		 
	</view>
</template>

<script>
	import {insertInvoice} from '@/api/invoice-api.js'
	export default {
		data() {
			return {
				bizNo: '',
				bizType: 0,
				totalPrice: 0,
				invoiceTypes: [{
					name: '普票',
					value: '1'
				}, {
					name: '专票',
					value: '2'
				}],
				currentInvoiceType: '1',
				
				currentInvoiceTitleType: 0,
				invoiceWays: [{
					name: '电子发票',
					value: '1'
				}, {
					name: '纸质发票',
					value: '2'
				}],
				currentInvoiceWay: '1',

				invoiceTitle: '',
				dutyParagraph: '',
				invoiceTitleId: '',
				email: '',
				consignee: '',
				consigneeAddress: '',
				contactPhone: '',
				showInvoiceAlert : false
			}
		},
		onLoad(op) {
			this.bizNo = op.bizNo;
			this.bizType = op.bizType;
			this.totalPrice = op.totalPrice;
			
			uni.$on('selectedInvoiceTitle', (item)=> {
				this.invoiceTitle = item.invoiceTitleName;
				this.invoiceTitleId = item.invoiceTitleId;
				this.dutyParagraph = item.dutyParagraph;
				this.currentInvoiceTitleType = item.invoiceTitleType;
			})
			
		},
		methods: {
			invoiceTypeRadioChange(e) {
				this.currentInvoiceType = e.target.value;
			},
			invoiceWayeRadioChange(e) {
				this.currentInvoiceWay = e.target.value;
			},
			//选择发票抬头
			chooseInvoiceTitle() {
				uni.navigateTo({
					url: './invoice-chooseHeader'
				})
			},
			next() {
				if(this.currentInvoiceTitleType == 0) {
					this.$u.toast('请选择发票抬头');
					return
				}else {
					if(this.currentInvoiceWay=='1') {//电子发票
						if(this.$u.test.isEmpty(this.email)) {
							this.$u.toast('请输入电子邮箱');
							return
						}
					}else if(this.currentInvoiceWay == '2') {//纸质发票
						if(this.$u.test.isEmpty(this.consignee)) {
							this.$u.toast('请输入收件人');
							return
						}
						if(this.$u.test.isEmpty(this.consigneeAddress)) {
							this.$u.toast('请输入收件人地址');
							return
						}
						if(this.$u.test.isEmpty(this.contactPhone)) {
							this.$u.toast('请输入收件人联系电话');
							return
						}
					}
				}
				this.showInvoiceAlert = true;
				
			},
			//提交开票
			commitInvoice() {
				uni.showLoading();
				insertInvoice({
					invoiceType : this.currentInvoiceType,
					invoiceAmount : this.totalPrice,
					invoiceTitle : this.invoiceTitle,
					bizNo : this.bizNo,
					bizType : this.bizType,
					invoiceTitleType : this.currentInvoiceTitleType,
					dutyParagraph : this.dutyParagraph,
					invoiceTitleId : this.invoiceTitleId,
					invoiceWay : this.currentInvoiceWay,
					email : this.email,
					consignee : this.consignee,
					consigneeAddress : this.consigneeAddress,
					contactPhone : this.contactPhone
				}).then(res=> {
					uni.hideLoading();
					if(res.code == 200) {
						uni.redirectTo({
							url: './invoice-applySuccess'
						})
					}
				})
			}
		},
		
		
	}
</script>

<style lang="scss" scoped>
	.invoiceWrapper {
		padding-bottom: 150upx;
		overflow: hidden;
	}
	.invoiceItemWrapper {
		margin: 40upx 20upx 0;

		.title {
			color: $text-color-33;
			font-size: 26upx;
			font-weight: bold;

			&::before {
				content: '';
				display: inline-block;
				width: 8upx;
				height: 26upx;
				border-radius: 2;
				background-color: $main-color;
				position: relative;
				top: 4upx;
				margin-right: 10upx;
			}
		}

		.content {
			margin-top: 30upx;
			background-color: $bg-white-color;
			padding: 30upx 44upx;
			border-radius: 16upx;
			box-shadow: 0px 0px 0px NaNpx rgba(69, 69, 69, 0.13);
			.invoicePrice {
				display: flex;
				justify-content: space-between;
				color: $text-color-33;
				font-size: 26upx;
				font-weight: bold;
				text:nth-child(2) {
					color: $text-color-price;
				}
			}
			
			.titleInfo {
				display: flex;
				justify-content: space-between;
				align-items: center;
				.subtitle {
					font-size: 26upx;
					font-weight: bold;
					color: $text-color-33;
					&::after {
						content: '*';
						color: $main-color;
						font-size: 26upx;
						
					}
				}
				.contentItem {
					padding: 10upx 0;
					text{
						font-size: 26upx;
						font-weight: bold;
						color: $text-color-33;
					}
					text:nth-child(2) {
						margin-left: 20upx;
						color: $text-color-66;
					}
				}
			}
			
		}
	}


	.radioBox {
		margin-right: 100upx;
		color: $text-color-33;
		font-size: 26upx;
		font-weight: bold;
	}

	.infoItem {
		display: flex;
		align-items: center;
		padding: 6upx 0;
		text {
			font-size: 26upx;
			font-weight: bold;
			color: $text-color-33;
			margin-right: 20upx;
		}
		.chooseInvoiceTitle {
			color: $main-color;
		}
		.subtitle {
			&::after {
				content: '*';
				color: $main-color;
				font-size: 26upx;
				
			}
		}
		
	}

	.bottomBtn {
		margin: 0 20upx;
		position: fixed;
		bottom: 20upx;
		width: 710upx;
		height: 90upx;
		line-height: 90upx;
		text-align: center;
		color: $text-color-ff;
		font-size: 30upx;
		font-weight: bold;
		background: linear-gradient(to right, #32ace9 0%, #176cfb 100%);
		border-radius: 10upx;
		z-index: 2;
	}
	
	.inviceAlertWrapper {
		background-color: $bg-white-color;
		padding-top: 10upx;
		.contentWrapper {
			padding: 44upx;
			.title {
				color: $text-color-33;
				font-size: 32upx;
				font-weight: bold;
			}
			.invoiceContent {
				margin-top: 20upx;
				.invoiceItem {
					display: flex;
					justify-content: space-between;
					// margin-bottom: 30upx;
					padding: 10upx 0;
					text{
						color: $text-color-33;
						font-size: 28upx;
						font-weight: bold;
					}
					text:nth-child(1){
						color: $text-color-99;
					}
				}
				.remind {
					// margin-top: 30upx;
					color: $text-color-99;
					font-size: 22upx;
					text {
						display: block;
					}
				}
			}
		}
		.buttonWrapper {
			height: 120upx;
			line-height: 120upx;
			text-align: center;
			box-shadow: 0px 0px 0px NaNpx #ebebeb; 
			.button {
				margin: 0 20upx;
				height: 90upx;
				line-height: 90upx;
				text-align: center;
				color: $text-color-ff;
				font-size: 30upx;
				font-weight: bold;
				background: linear-gradient(to right, #32ace9 0%, #176cfb 100%);
				border-radius: 10upx;
			}
		}
		
	}
</style>
